package com.binance.rabbitmq.processor;

import lombok.Data;
import lombok.Singular;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by robin.wu on 2018/11/29.
 */
@Data
public class InfoHeaderMessagePostProcessor implements MessagePostProcessor {

    @Singular
    private Map<String, Object> headers = new HashMap<>();

    @Autowired
    private Environment environment;

    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        MessageProperties messageProperties = message.getMessageProperties();
        headers.putIfAbsent("spring-application-name",
                getEnvironment().getProperty("spring.application.name", String.class));
        headers.put("execution-time", new Date().toString());
        messageProperties.getHeaders().putIfAbsent("info", headers);
        return message;
    }
}
